package cn.edu.njtc.service.impl;

import cn.edu.njtc.entity.pojo.PatientAdverseReaction;
import cn.edu.njtc.entity.vo.PatientAdverseReactionVo;
import cn.edu.njtc.mapper.PatientAdverseReactionMapper;
import cn.edu.njtc.service.PatientAdverseReactionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PatientAdverseReactionServiceImpl extends ServiceImpl<PatientAdverseReactionMapper, PatientAdverseReaction> implements PatientAdverseReactionService {

    @Override
    public List<PatientAdverseReactionVo> getReactionsByUserId(Integer userId) {
        LambdaQueryWrapper<PatientAdverseReaction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatientAdverseReaction::getUserId, userId)
                .eq(PatientAdverseReaction::getDeleted, false);

        return list(queryWrapper).stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

    @Override
    public PatientAdverseReactionVo getReactionById(Integer recordId) {
        PatientAdverseReaction reaction = getById(recordId);
        if (reaction == null || reaction.getDeleted()) {
            return null;
        }
        return convertToVo(reaction);
    }

    @Override
    public boolean addReaction(PatientAdverseReaction reaction) {
        return save(reaction);
    }

    @Override
    public boolean updateReaction(PatientAdverseReaction reaction) {
        return updateById(reaction);
    }

    @Override
    public boolean deleteReaction(Integer recordId) {
        return removeById(recordId);
    }

    private PatientAdverseReactionVo convertToVo(PatientAdverseReaction reaction) {
        PatientAdverseReactionVo vo = new PatientAdverseReactionVo();
        BeanUtils.copyProperties(reaction, vo);
        return vo;
    }
}
